﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChronoScript_Compiler.Language;

namespace ChronoScript_Compiler.Language.Parser
{
    public partial class Parser
    {
        public LanguageDefinition.SyntaxNode parseLine()
        {
            while (PeekToken() is Tokens.EOL)
            {
                ReadToken();
            }

            if (PeekToken().ToString().ToLower() == "if" && PeekToken(1) is Tokens.openParenthesis)
            {
                return ParseIf();
            }
            else if (PeekToken().ToString() == "while" && PeekToken(1) is Tokens.openParenthesis)
            {
                ReadToken(); ReadToken();
                LanguageDefinition.WhileStatement whilestat = new LanguageDefinition.WhileStatement();
                whilestat.Comparison = ParseExpression();
                if (!(PeekToken() is Tokens.closeParenthesis))
                    Console.WriteLine("Expected ) at " + ReadToken().lineNumber);
                if (!(PeekToken() is Tokens.openCurlyBracket))
                {
                    Console.WriteLine("Excepted { at " + ReadToken().lineNumber);
                }
                whilestat.Body = ParseBlock();
                return whilestat;
            }
            else if (PeekToken().ToString() == "asm" && PeekToken(1) is Tokens.openParenthesis && PeekToken(2) is Tokens.StringLiteral && PeekToken(3) is Tokens.closeParenthesis)
            {
                ReadToken();
                ReadToken();
                string IL = ((Tokens.StringLiteral)ReadToken()).Value; ReadToken();
                LanguageDefinition.InlineIL code = new LanguageDefinition.InlineIL();
                code.IL_Code = IL;
                if (!(ReadToken() is Tokens.SemiColon))
                {
                    Console.WriteLine("Excepted ; at " + PeekToken().lineNumber);
                }
                return code;
            }
            else if (PeekToken() is Tokens.Statement && PeekToken(1) is Tokens.Statement)
            {
                LanguageDefinition.Declaration decl = ParseDeclaration();
                if (!(ReadToken() is Tokens.SemiColon))
                {
                    Console.WriteLine("Excepted ; at " + PeekToken().lineNumber);
                }
                return decl;
            }
            else if (PeekToken() is Tokens.Statement && PeekToken(1) is Tokens.Assign)
            {
                LanguageDefinition.Assignment assignment = new LanguageDefinition.Assignment();
                assignment.Name = ReadToken().ToString();
                ReadToken();
                assignment.Value = ParseExpression();
                if (!(PeekToken() is Tokens.SemiColon))
                {
                    Console.WriteLine("Excepted ; at " + ReadToken().lineNumber);
                }
                Console.WriteLine(assignment.Name + " " + (assignment.Value.Items[0] as LanguageDefinition.StringLiteral).value);
                return assignment;
            }
            else if (PeekToken() is Tokens.Statement && PeekToken(1) is Tokens.openParenthesis)
            {
                LanguageDefinition.FunctionCall call = Call();
                if (!(ReadToken() is Tokens.SemiColon))
                {
                    Console.WriteLine("Excepted ; at " + PeekToken().lineNumber);
                }
                return call;
            }
            return new LanguageDefinition.SyntaxNode();
        }
    }
}
